//
//  UIImage+extension.swift
//  MySinaWeiBoSwiftProject
//
//  Created by apple on 2016/11/21.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit

extension UIImage{
    
    class func snapCurrentScreenImage()->UIImage {
        
        let window = UIApplication.shared.keyWindow!
        
        UIGraphicsBeginImageContextWithOptions(window.frame.size, false, 0)
        
        window.drawHierarchy(in: window.frame, afterScreenUpdates: true)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        return image!
        
    }
    
    // MARK:
    // MARK: - 得到小图片
    func imageWithScaleWidth(width: CGFloat) -> UIImage {
        
        let imageWidth: CGFloat = self.size.width
        let imageHeight: CGFloat = self.size.height
        
        if(imageWidth < width){
            return self
        }
        
        let newWidth = width
        let newHeight = newWidth * imageHeight / imageWidth
        
        let imageBounds = CGRect(x: 0, y: 0, width: newWidth, height: newHeight)
        
        UIGraphicsBeginImageContextWithOptions(imageBounds.size, false, 0)
        
        self.draw(in: imageBounds)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()

        UIGraphicsEndImageContext()
        
        return image!
    }
    
    
}
